import React, { Component, Fragment } from 'react'
export default (storeArr) => (ProxyStore, storeKayArr) => {
    if (!(storeKayArr instanceof Array)) throw 'The second parameter to creatProxy must be of array string type'
    const proxStores = {}, store = { data: {}, actions: {} }
    storeArr.forEach(storeCompenent => {
        storeKayArr.forEach(key => {
            if (key === storeCompenent.storeName) {
                proxStores[key] = storeCompenent
                storeCompenent.data ? store.data[key] = storeCompenent.data : ''
                storeCompenent.actions ? store.actions[key] = storeCompenent.actions : ''
            }
        })
    })
    const functionFactory = (funName, value, storeName, proxyComponents) => {
        if (!proxStores[storeName] || !proxStores[storeName][funName]) return
        proxStores[storeName][funName](value, proxyComponents)
    }
    const busFunctionFactory = (funName, storeName, value1, value2, value3) => {
        if (!proxStores[storeName] || !proxStores[storeName][funName]) return
        if (value1 && value2) {
            proxStores[storeName][funName](value1, value2)
        } else if (value1 && value3) {
            proxStores[storeName][funName](value1, ...value3)
        } else if (value1) {
            proxStores[storeName][funName](value1)
        }
    }
    class ProxStore extends Component {
        constructor(props) {
            super(props)
            store.bindData = (dataKeysArr, storeName) => functionFactory('bindData', dataKeysArr, storeName, this)
            store.unBindData = (dataKeysArr, storeName) => functionFactory('unBindData', dataKeysArr, storeName, this)
            store.responseCallback = (fun, storeName) => functionFactory('responseCallback', fun, storeName)
            store.changeData = (data, storeName) => functionFactory('changeData', data, storeName)
            store.clearData = (dataKeys, storeName) => functionFactory('clearData', dataKeys, storeName)
            store.mapListState = (dataKey, storeName) => {
                if (!proxStores[storeName] || !proxStores[storeName].mapData) return
                const bindFun = {}
                proxStores[storeName].mapListState(dataKey, bindFun)
                return bindFun
            }

            store.pushState = (str, fun, storeName) => busFunctionFactory('pushState', storeName, str, fun)
            store.changeState = (changArr, storeName) => busFunctionFactory('changeState', storeName, changArr[0], undefined, changArr.slice(1))
            store.clearMethods = (mothodsNameArr, isAsync, storeName) => busFunctionFactory('clearMethods', storeName, mothodsNameArr, isAsync)
            store.asyncPushState = (str, fun, storeName) => busFunctionFactory('asyncPushState', storeName, str, fun)
            store.asyncChangeState = async (changArr, storeName) => {
                if (!proxStores[storeName] || !proxStores[storeName].asyncChangeState) return
                return await proxStores[storeName].asyncChangeState(changArr[0], ...changArr.slice(1))
            }

        }
        render() {
            return (
                <Fragment>
                    <ProxyStore {...this.props} rStore={store} storeHouse={proxStores} />
                </Fragment>
            )
        }
    }
    return ProxStore
}